# -*- coding: utf-8 -*-
import os
import xml.dom.minidom as xmldom
import time
print ('运行啦')
time.sleep(10)
# //电源键
# adb shell input keyevent 26
# //菜单键
# adb shell input keyevent 82
# //home键
# adb shell input keyevent 3
# //返回键
# adb shell input keyevent 4
# //打开微信
# adb shell am start -n com.tencent.mm/.ui.LauncherUI
# //模拟点击 100 300是坐标
# adb shell input tap 100 300
# //模拟滑动 300 1000 300 500 分别表示起始点x坐标 起始点y坐标 结束点x坐标 结束点y坐标。
# adb shell input swipe 300 1000 300 500
# //模拟输入 hello
# adb shell input text hello
# //拨打电话 10086
# adb shell am start -a android.intent.action.CALL tel:10086
# //编辑短信hello(不支持中文,不过好像有方法做到,貌似需要在手机安装特殊的输入法,暂不搞)
# adb shell am start -a android.intent.action.SENDTO -d sms:10086 --es sms_body hello
# adb shell input keyevent 22  //焦点去到发送按键
# adb shell input keyevent 66  //回车，就是按下发送键
# ---------------------
File_DIR = os.path.dirname(os.path.abspath(__file__))

# 获取要点击的点
def getPoint(attr,value_str):
    os.system('adb shell uiautomator dump --compressed /data/local/tmp/uidump.xml')
    os.system('adb pull /data/local/tmp/uidump.xml ' + str(File_DIR))
    xmlfilepath = os.path.abspath("uidump.xml")

    # 得到文档对象
    domobj = xmldom.parse(xmlfilepath)
    # print(u"xmldom.parse:", type(domobj))

    # 得到元素对象
    elementobj = domobj.documentElement
    # print ("domobj.documentElement:", type(elementobj))

    # 获得子标签
    nodes = elementobj.getElementsByTagName("node")

    # print ("getElementsByTagName:", type(nodes))
    # 打印每部电影的详细信息
    for node in nodes:
        if node.hasAttribute(attr):
            print ("Title: %s" % node.getAttribute(attr))

            if node.getAttribute(attr) == value_str:
                bounds_str = str(node.getAttribute('bounds'))
                bounds_str = bounds_str.replace('][', ',')
                bounds_str = bounds_str.replace(']', '')
                bounds_str = bounds_str.replace('[', '')
                list_str = bounds_str.split(',')
                # (point,size) = node.getAttribute('bounds')
                x = int(list_str[0])
                y = int(list_str[1])
                w = int(list_str[2])
                h = int(list_str[3])
                print ('找到了 point', bounds_str)
                print ('return point', x + (w-x)/2, y + (h-y)/2)
                return x + (w-x)/2, y + (h-y)/2
    print ('未找到', value_str)
    return None, None


#
#
#
# os.system('adb shell am start -n com.tencent.mm/.ui.LauncherUI')
# time.sleep(0.5)
# x,y= getPoint(u'content-desc',u'更多功能按钮')#加号
# os.system('adb shell input tap '+str(x)+' '+str(y))

# x,y= getPoint(u'text',u'添加朋友')#添加朋友
# if x is None:
#     quit()
# os.system('adb shell input tap '+str(x)+' '+str(y))

# x,y= getPoint(u'text',u'微信号/QQ号/手机号')#点击搜索框
# if x is None:
#     quit()
# os.system('adb shell input tap '+str(x)+' '+str(y))
# time.sleep(0.5)
# os.system('adb shell input text 18354158762')#手机号
# time.sleep(1)
# x,y= getPoint(u'text',u'搜索:'+str(18354158762))#点击搜索按钮
# if x is None:
#     quit()
# os.system('adb shell input tap '+str(x)+' '+str(y))

# x,y= getPoint(u'text',u'添加到通讯录')#点击搜添加好友
# if x is None:
#     quit()
# os.system('adb shell input tap '+str(x)+' '+str(y))

# os.system('adb shell input keyevent 66')
# time.sleep(0.5)
# os.system('adb shell input keyevent 66')
# time.sleep(0.5)
# os.system('adb shell input keyevent 66')
# time.sleep(0.5)
# os.system('adb shell input tap 500 830')
# os.system('')
# os.system('')
# os.system('')
# os.system('')
# os.system('')
